package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.Update;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.dubbo.mappers.*;
import com.tanhua.model.domain.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class SmsHomeBrandApiImpl implements SmsHomeBrandApi{

    @Autowired
    private SmsHomeBrandMapper smsHomeBrandMapper;

    @Autowired
    private SmsHomeNewProductMapper smsHomeNewProductMapper;

    @Autowired
    private SmsHomeRecommendProductMapper smsHomeRecommendProductMapper;

    @Autowired
    private SmsHomeRecommendSubjectMapper smsHomeRecommendSubjectMapper;

    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    /**
     * 分页查询推荐品牌
     * @param brandName
     * @param recommendStatus
     * @param page
     * @return
     */
    @Override
    public Page<SmsHomeBrand> brandList(String brandName, Integer recommendStatus, Page<SmsHomeBrand> page) {
        LambdaQueryWrapper<SmsHomeBrand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(brandName!=null,SmsHomeBrand::getBrandName,brandName)
                .eq(recommendStatus!= null,SmsHomeBrand::getRecommendStatus,recommendStatus)
                .orderByDesc(SmsHomeBrand::getSort);
        return smsHomeBrandMapper.selectPage(page,lambdaQueryWrapper);
    }

    /**
     * 批量修改推荐品牌状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @Override
    public void updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        for (Long id : ids) {
            SmsHomeBrand smsHomeBrand = new SmsHomeBrand();
            smsHomeBrand.setId(id);
            smsHomeBrand.setRecommendStatus(recommendStatus);
            smsHomeBrandMapper.updateById(smsHomeBrand);
        }

    }

    /**
     * 修改推荐品牌排序
     * @param smsHomeBrand
     * @return
     */
    @Override
    public void updateSort(SmsHomeBrand smsHomeBrand) {
        smsHomeBrandMapper.updateById(smsHomeBrand);
    }

    /**
     * 添加首页推荐品牌
     * @param smsHomeBrand
     * @return
     */
    @Override
    public void create(List<SmsHomeBrand> smsHomeBrand) {
        //判断表有没有,有就不加了
        List<Long> brandIds = smsHomeBrand.stream().map(s -> {
            return s.getBrandId();
        }).collect(Collectors.toList());

        LambdaQueryWrapper<SmsHomeBrand> lqw = new LambdaQueryWrapper<>();
        lqw.in(lqw != null,SmsHomeBrand::getBrandId,brandIds);
        List<SmsHomeBrand> smsHomeBrands = smsHomeBrandMapper.selectList(lqw);
        if (smsHomeBrands != null && smsHomeBrands.size() !=0){
            throw new BusinessException("选择的品牌已经在表中");
        }

        for (SmsHomeBrand homeBrand : smsHomeBrand) {
            smsHomeBrandMapper.insert(homeBrand);
        }

    }

    @Override
    public void delete(List<Integer> ids) {
        smsHomeBrandMapper.deleteBatchIds(ids);
    }



    //============================================新品推荐

    /**
     * 分页查询首页新品
     * @param productName
     * @param recommendStatus
     * @param page
     * @return
     */
    @Override
    public Page newProductList(String productName, Integer recommendStatus, Page<SmsHomeNewProduct> page) {
        LambdaQueryWrapper<SmsHomeNewProduct> lqw = new LambdaQueryWrapper<>();
        lqw.like(productName != null,SmsHomeNewProduct::getProductName,productName)
                .in(recommendStatus != null,SmsHomeNewProduct::getRecommendStatus,recommendStatus)
                .orderByDesc(SmsHomeNewProduct::getSort);
        return smsHomeNewProductMapper.selectPage(page,lqw);
    }

    /**
     * 添加首页新品
     * @param smsHomeNewProduct
     */
    @Override
    public void createNewProduct(List<SmsHomeNewProduct> smsHomeNewProduct) {

        //判断表有没有,有就不加了
        List<Long> productIds= smsHomeNewProduct.stream().map(s -> {
            return s.getProductId();
        }).collect(Collectors.toList());

        LambdaQueryWrapper<SmsHomeNewProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(lqw != null,SmsHomeNewProduct::getProductId,productIds);

        List<SmsHomeNewProduct> smsHomeNewProducts = smsHomeNewProductMapper.selectList(lqw);
        if (smsHomeNewProducts != null && smsHomeNewProducts.size() !=0){
            throw new BusinessException("选择的商品已经在表中");
        }

        for (SmsHomeNewProduct shnr : smsHomeNewProduct) {
            smsHomeNewProductMapper.insert(shnr);
        }

    }

    /**
     * 批量删除首页新品
     * @param ids
     * @return
     */
    @Override
    public void deleteNewProduct(List<Integer> ids) {
        smsHomeNewProductMapper.deleteBatchIds(ids);
    }

    /**
     * 批量修改首页新品状态   是否推荐
     * @param ids
     * @param recommendStatus
     * @return
     */
    @Override
    public void updateNewProductRecommendStatus(List<Long> ids, Integer recommendStatus) {
        for (Long id : ids) {
            SmsHomeNewProduct smsHomeBrand = new SmsHomeNewProduct();
            smsHomeBrand.setId(id);
            smsHomeBrand.setRecommendStatus(recommendStatus);
            smsHomeNewProductMapper.updateById(smsHomeBrand);
        }
    }

    /**
     * 修改首页新品排序
     * @param smsHomeNewProduct
     * @return
     */
    @Override
    public void updateNewProductSort(SmsHomeNewProduct smsHomeNewProduct) {
        smsHomeNewProductMapper.updateById(smsHomeNewProduct);
    }

    //============================================人气推荐

    /**
     * 分页查询人气推荐
     * @param productName
     * @param recommendStatus
     * @param page
     * @return
     */
    @Override
    public Page recommendProductList(String productName, Integer recommendStatus, Page<SmsHomeRecommendProduct> page) {
        LambdaQueryWrapper<SmsHomeRecommendProduct> lqw = new LambdaQueryWrapper<>();
        lqw.like(productName != null,SmsHomeRecommendProduct::getProductName,productName)
                .in(recommendStatus != null,SmsHomeRecommendProduct::getRecommendStatus,recommendStatus)
                .orderByDesc(SmsHomeRecommendProduct::getSort);
        return smsHomeRecommendProductMapper.selectPage(page,lqw);
    }

    /**
     * 添加人气推荐
     * @param smsHomeRecommendProduct
     * @return
     */
    @Override
    public void createRecommendProduct(List<SmsHomeRecommendProduct> smsHomeRecommendProduct) {

        //判断表有没有,有就不加了
        List<Long> productIds= smsHomeRecommendProduct.stream().map(s -> {
            return s.getProductId();
        }).collect(Collectors.toList());

        LambdaQueryWrapper<SmsHomeRecommendProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(lqw != null,SmsHomeRecommendProduct::getProductId,productIds);

        List<SmsHomeRecommendProduct> smsHomeNewProducts = smsHomeRecommendProductMapper.selectList(lqw);
        if (smsHomeNewProducts != null && smsHomeNewProducts.size() !=0){
            throw new BusinessException("选择的商品已经在表中");
        }

        for (SmsHomeRecommendProduct item : smsHomeRecommendProduct) {
            smsHomeRecommendProductMapper.insert(item);
        }
    }


    /**
     * 批量删除人气推荐
     * @param ids
     * @return
     */
    @Override
    public void deleteRecommendProduct(List<Integer> ids) {
        smsHomeRecommendProductMapper.deleteBatchIds(ids);
    }

    /**
     * 批量修改人气推荐   是否推荐
     * @param ids
     * @param recommendStatus
     * @return
     */
    @Override
    public void updateRecommendProductRecommendStatus(List<Long> ids, Integer recommendStatus) {
        for (Long id : ids) {
            SmsHomeRecommendProduct smsHomeRecommendProduct = new SmsHomeRecommendProduct();
            smsHomeRecommendProduct.setId(id);
            smsHomeRecommendProduct.setRecommendStatus(recommendStatus);
            smsHomeRecommendProductMapper.updateById(smsHomeRecommendProduct);
        }
    }


    /**
     * 修改人气排序
     * @param smsHomeRecommendProduct
     * @return
     */
    @Override
    public void updateRecommendProductSort(SmsHomeRecommendProduct smsHomeRecommendProduct) {
        smsHomeRecommendProductMapper.updateById(smsHomeRecommendProduct);
    }



    //============================================专题推荐
    /**
     * 分页查询专题推荐
     * @param subjectName
     * @param recommendStatus
     * @param page
     * @return
     */
    @Override
    public Page recommendSubjectList(String subjectName, Integer recommendStatus, Page<SmsHomeRecommendSubject> page) {
        LambdaQueryWrapper<SmsHomeRecommendSubject> lqw = new LambdaQueryWrapper<>();
        lqw.like(subjectName != null,SmsHomeRecommendSubject::getSubjectName,subjectName)
                .in(recommendStatus != null,SmsHomeRecommendSubject::getRecommendStatus,recommendStatus)
                .orderByDesc(SmsHomeRecommendSubject::getSort);

        return smsHomeRecommendSubjectMapper.selectPage(page,lqw);
    }

    /**
     * 添加首页推荐专题
     * @param smsHomeRecommendSubject
     * @return
     */
    @Override
    public void recommendSubjectCreate(List<SmsHomeRecommendSubject> smsHomeRecommendSubject) {
        //判断表有没有,有就不加了
        List<Long> subjectIds= smsHomeRecommendSubject.stream().map(s -> {
            return s.getSubjectId();
        }).collect(Collectors.toList());

        LambdaQueryWrapper<SmsHomeRecommendSubject> lqw = new LambdaQueryWrapper<>();
        lqw.in(lqw != null,SmsHomeRecommendSubject::getSubjectId,subjectIds);

        List<SmsHomeRecommendSubject> smsHomeNewProducts = smsHomeRecommendSubjectMapper.selectList(lqw);
        if (smsHomeNewProducts != null && smsHomeNewProducts.size() !=0){
            throw new BusinessException("选择的商品已经在表中");
        }

        for (SmsHomeRecommendSubject item : smsHomeRecommendSubject) {
            smsHomeRecommendSubjectMapper.insert(item);
        }

    }

    /**
     * 批量删除专题推荐
     * @param ids
     * @return
     */
    @Override
    public void deleteRecommendSubject(List<Integer> ids) {
        smsHomeRecommendSubjectMapper.deleteBatchIds(ids);
    }


    /**
     * 批量修改专题推荐   是否推荐
     * @param ids
     * @param recommendStatus
     * @return
     */
    @Override
    public void updateRecommendSubjectRecommendStatus(List<Long> ids, Integer recommendStatus) {
        for (Long id : ids) {
            SmsHomeRecommendSubject smsHomeRecommendSubject = new SmsHomeRecommendSubject();
            smsHomeRecommendSubject.setId(id);
            smsHomeRecommendSubject.setRecommendStatus(recommendStatus);
            smsHomeRecommendSubjectMapper.updateById(smsHomeRecommendSubject);
        }

    }

    /**
     * 修改专题推荐排序
     * @param smsHomeRecommendSubject
     * @return
     */
    @Override
    public void updateRecommendSubjectSort(SmsHomeRecommendSubject smsHomeRecommendSubject) {
        smsHomeRecommendSubjectMapper.updateById(smsHomeRecommendSubject);
    }


    //============================================广告列表

    /**
     * 分页查询广告列表
     * @param endTime
     * @param type
     * @param name
     * @param page
     * @return
     */
    @Override
    public Page advertiseList(String endTime, Integer type, String name, Page<SmsHomeAdvertise> page) {
        LambdaQueryWrapper<SmsHomeAdvertise> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.lt(endTime != null,SmsHomeAdvertise::getEndTime,endTime)
                .like(name !=null,SmsHomeAdvertise::getName,name)
                .lt(type !=null,SmsHomeAdvertise::getType,type)
                .orderByDesc(SmsHomeAdvertise::getSort);
        return smsHomeAdvertiseMapper.selectPage(page,lambdaQueryWrapper);

    }

    /**
     * 添加广告
     * @param smsHomeAdvertise
     * @return
     */
    @Override
    public void advertiseCreate(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseMapper.insert(smsHomeAdvertise);
    }

    /**
     * 批量删除广告
     * @param ids
     */
    @Override
    public void deleteAdvertise(List<Integer> ids) {
        smsHomeAdvertiseMapper.deleteBatchIds(ids);
    }

    /**
     * 修改广告上下线状态
     * @param smsHomeAdvertise
     */
    @Override
    public void updateAdvertiseStatus(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
    }

    /**
     * 修改广告
     * @param smsHomeAdvertise
     * @return
     */
    @Override
    public void updateAdvertise(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
    }

    /**
     * 获取广告详情
     * @param id
     * @return
     */
    @Override
    public SmsHomeAdvertise getAdvertiseById(Long id) {
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseMapper.selectById(id);
        if (smsHomeAdvertise == null){
            return null;
        }
        return smsHomeAdvertise;
    }
}
